package com.hp.bon.sgw.stdbss30;

import java.io.PrintStream;
import java.text.SimpleDateFormat;

import org.simpleframework.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.core.channel.SoapChannel;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.util.Comutils;

public class OutApiResponsor {

	private static final Logger _log = LoggerFactory.getLogger(OutApiResponsor.class);

	public void doResponse(BizResult bizResult, Response response) {

		try {
			PrintStream body = response.getPrintStream();
			byte[] resps = bizResult.getHttpBody().getBytes("UTF-8");
			body.write(resps);
			body.close();
			return;
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}

	}

	public void doResponseWSError(Response response, String mid, int retCode, String errMsg) {

		PrintStream printStream = null;
		try {
			printStream = response.getPrintStream();
		} catch (Exception e) {
			_log.error("response fail! mid={}", mid);
			_log.error(e.getMessage(), e);
			return;
		}

		try {
			XmlMessage ack = new XmlMessage(false);
			ack.setMID(mid);
			ack.setRTT(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Long.valueOf(System.currentTimeMillis())));
			ack.setRC(Integer.toString(retCode));
			ack.setDS(errMsg);

			_log.error(JSON.toJSONString(ack));

			SoapChannel.directAck(ack, printStream);

			_log.error("direct ack, mid={}", mid);
		} catch (Exception e) {
			_log.error(e.getMessage(), e);
		}

	}

	public void doResponseStandardHttpError(Response response, String sig, String rc, String ds, String data) {

		PrintStream printStream = null;
		try {
			printStream = response.getPrintStream();
		} catch (Exception e) {
			_log.error("response fail! sig={}", sig);
			_log.error(e.getMessage(), e);
			return;
		}

		String ack = Comutils.genTuxedoRspsXmlStr(sig, rc, ds, data);
		_log.info("send http ack {}", ack);
		try {
			printStream.write(ack.getBytes("UTF-8"));
			printStream.close();
		} catch (Exception e) {
			_log.warn("caught err:", e);
		}

	}

	public void doResponseError(Response response, String data) {

		PrintStream printStream = null;
		try {
			printStream = response.getPrintStream();
		} catch (Exception e) {
			_log.error("response fail! data={}", data);
			_log.error(e.getMessage(), e);
			return;
		}

		_log.info("send http data: ", data);
		try {
			printStream.write(data.getBytes("UTF-8"));
			printStream.close();
		} catch (Exception e) {
			_log.warn("caught err:", e);
		}

	}

//	
//	public void doExceptionResponse(BizResult bizResult, Response response) {
//
//		try {
//			PrintStream body = response.getPrintStream();
//			byte[] resps = bizResult.getHttpBody().getBytes("UTF-8");
//			body.write(resps);
//			body.close();
//		} catch (Exception e) {
//			_log.error(e.getMessage(), e);
//		}
//
//	}
//	

}
